package com.freewater.springboot.demo.aop.api.targetsource.swappable;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.target.HotSwappableTargetSource;

import java.io.Serial;

public class Main {
    public static void main(String[] args) {
        ProxyFactory proxyFactory = new ProxyFactory();

        BusinessService service = new BusinessServiceImpl1();
        HotSwappableTargetSource targetSource = new HotSwappableTargetSource(service) {
            @Serial
            private static final long serialVersionUID = 170913878666429814L;

            @Override
            public synchronized Class<?> getTargetClass() {
                return BusinessService.class;
            }
        };
        proxyFactory.setTargetSource(targetSource);

        BusinessService proxy = (BusinessService) proxyFactory.getProxy();
        proxy.doSomething(); // BusinessServiceImpl1 do something()

        targetSource.swap(new BusinessServiceImpl2());
        proxy.doSomething(); // BusinessServiceImpl2 do something()
    }
}
